﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ch01
{
    public partial class FrmDataGridView : Form
    {
        public FrmDataGridView()
        {
            InitializeComponent();
        }

        private void FrmDataGridView_Load(object sender, EventArgs e)
        {
            BoinComdoBox();
            this.dataGridView1.AutoGenerateColumns = false;
            BoinDataGridViem();
        }
        public void BoinComdoBox()
        {
            string sql = "select * from grade";
            DataTable dt=DBHelper.SelectTable(sql);
            DataRow dr = dt.NewRow();
            dr["gradename"] = "全部" ;
            dr["no"]=0;
            dt.Rows.InsertAt(dr,0);
            this.comboBox1.DisplayMember = "gradename";
            this.comboBox1.ValueMember = "no";
            this.comboBox1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BoinDataGridViem();
            if (dataGridView1.SelectedRows.Count<=0)
            {
                MessageBox.Show("没有查询到数据！");
                return;
            }
        } 

        public void BoinDataGridViem()
        {
            string sql = "select * from student s,grade g where s.gradeid=g.no and 1=1";
            string name = this.textBox1.Text;
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and name like '%{0}%'",name);
            }
            int gradeid =Convert.ToInt32(this.comboBox1.SelectedValue);
            if (gradeid!=0)
            {
                sql += string.Format(" and s.gradeid={0}",gradeid);
            }
            this.dataGridView1.DataSource = DBHelper.SelectTable(sql);
        }


        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count<=0)
            {
                MessageBox.Show("至少选择一条数据信息！");
                return;
            }
            DialogResult dr = MessageBox.Show("您确定要删除吗？","提示信息！",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (dr==DialogResult.Yes)
            {
                object o = this.dataGridView1.SelectedRows[0].Cells[0].Value;
                int SutNo = Convert.ToInt32(o);
                string sql =string.Format("delete from student where sutno={0}",SutNo);
                int result = DBHelper.DataUpdate(sql);
                if (result > 0)
                {
                    MessageBox.Show("删除成功！");
                    BoinDataGridViem();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count<=0)
            {
                MessageBox.Show("至少选择一条数据信息");
                return;
            }
            object o = this.dataGridView1.SelectedRows[0].Cells[0].Value;
            int SutNo = Convert.ToInt32(o);
            FrmUpdateSutInfo fs = new FrmUpdateSutInfo();
            fs.sutNo = SutNo;
            fs.ShowDialog();
            BoinDataGridViem();
        }
    }
}
